extends Control
class_name FlyMap


const grid_size = 32

var x_c = 20
var y_c = 20

var x = 0
var y = 0

const data = {
	"grid_info": [[]]
}

onready var bg = $ScrollContainer/AspectRatioContainer/mapBg
onready var sign_box = $ScrollContainer/AspectRatioContainer/mapBg/signs
onready var cur_sign = $ScrollContainer/AspectRatioContainer/mapBg/CurentSign
onready var name_input = $Panel/VBoxContainer/name/LineEdit
onready var flay_op = $Panel/VBoxContainer/flay/OptionButton
onready var coord_box = $Panel/VBoxContainer/coord
onready var map_input = $Panel/VBoxContainer/flyMap/LineEdit
onready var map_op = $Panel/VBoxContainer/flyMap/OptionButton
onready var coord_x_input = $Panel/VBoxContainer/coord/HBoxContainer/SpinBox
onready var coord_y_input = $Panel/VBoxContainer/coord/HBoxContainer/SpinBox2


const Sign = preload("res://tool/page/flyMap/Sign.tscn")

func _ready():
	init_map()
	
	update_grid_data_color()




func init_map():
	var map_data = FileHelper.read(DataSource.word_map_data_info_path)

	var x = bg.rect_size.x
	var y = bg.rect_size.y
	
	x_c = int(x / grid_size)
	y_c = int(y / grid_size)
	
	
	var grid_info = []
	
	sign_box.columns = x_c
	
	for child in sign_box.get_children():
		child.queue_free()
		
	for i in range(x_c):
		grid_info.append([])
		for j in range(y_c):
			
			if map_data && i< map_data["grid_info"].size() && j<map_data["grid_info"][0].size() && map_data["grid_info"][i][j]:
				grid_info[i].append(map_data["grid_info"][i][j])
			else:
				grid_info[i].append(null)
			
			var sign_ui =  Sign.instance()
			sign_box.add_child(sign_ui)
			sign_ui.connect("click_sign", self, "_on_click_sign")
			
	
	data["grid_info"] = grid_info



# 更新格子范围
func update_grid_data_color():
	
	for child in sign_box.get_children():
		var index = child.get_index()
		var y = index / x_c
		var x = index % x_c 
		
		var grid_info = data["grid_info"][x][y]
		
		if grid_info:
			child.modulate = "#ffffff"
		else:
			child.modulate = "#ff0000"	

func _on_click_sign(index):
	y = index / x_c
	x = index % x_c 
	
	$Panel/VBoxContainer/id/IdValue.text = str(index+1)
	
	var grid_info = data["grid_info"][x][y]
	if grid_info:
		name_input.text = grid_info["name"]
		flay_op.select(flay_op.get_item_index(grid_info['flay']))
		
		var map_id = int(grid_info.get("map_id", "-1"))
		var select_id = map_op.get_item_index(map_id)
		
		map_op.select(select_id)
		
		coord_x_input.value = grid_info["x"]
		coord_y_input.value = grid_info["y"]
	else:
		name_input.text = ""
		flay_op.select(0)
		coord_x_input.value = 0
		coord_x_input.value = 0
		map_op.select(map_op.get_item_id(-1))
	
	
	# 飞行相关信息显示
	$Panel/VBoxContainer/flyMap.visible = flay_op.get_selected_id()
	coord_box.visible = flay_op.get_selected_id()
	
	set_selected_position(x, y)
	pass


# ==================================================
# 设置选择的位置
# ==================================================
func set_selected_position(x, y):
	cur_sign.rect_position = Vector2(x*grid_size, y*grid_size)



# 点击保存信息
func _on_save_button_up():
	var map_data = {
		"name": name_input.text,
		"flay": flay_op.get_selected_id(),
		"map_id": str(map_op.get_selected_id()),
		"x": coord_x_input.value,
		"y": coord_y_input.value,
	}
	
	
	if !map_data["name"] && !map_data["flay"]:
		data["grid_info"][x][y] = null
	else:
		data["grid_info"][x][y] = map_data
	
	FileHelper.save(DataSource.word_map_data_info_path, data)
	
	update_grid_data_color()


# 可飞行选择改变
func _on_OptionButton_item_selected(index):
	$Panel/VBoxContainer/flyMap.visible = flay_op.get_selected_id()
	coord_box.visible = flay_op.get_selected_id()


func _on_OptionButton_visibility_changed():
	if visible:
		map_op.clear()
		var maps = DataSource.get_data_list(DataSource.map_register)
		for map in maps:
			map_op.add_item(map["name"], map["id"])


static func get_op_list():
	var grid_info = data["grid_info"]
	
	var list = []
	
	if grid_info && grid_info.size()>1 && grid_info[0].size()>1:
		
		for i in range(grid_info.size()):
			for j in range(grid_info[0].size()): 
				var info = grid_info[i][j]
				if info:
					var id = j*grid_info.size()+i+1
					
					list.append({
						"name": info["name"],
						"id": id,
					})
	
	return list
